package ui;

import Bankmanagement.BankSystem;
import RestaurantManagement.RestaurantService;
import ShapeParsingAndAnalysis.ShapeService;
import Zoomanagement.ZooManagementSystem;

import javax.swing.*;
import java.awt.*;

public class UIIntegration extends JFrame {
    // System instances
    private ZooManagementSystem zooSystem;
    private BankSystem bankSystem;
    private RestaurantService restaurantSystem;
    private ShapeService shapeSystem;
    private JLabel statusLabel;

    public UIIntegration() {
        super("Integrated Management System");
        initializeSystems();
        setupMainUI();
    }

    private void initializeSystems() {
        zooSystem = new ZooManagementSystem();
        bankSystem = new BankSystem();
        restaurantSystem = new RestaurantService();
        shapeSystem = new ShapeService();

        // Initialize bank account for transactions
        try {
            bankSystem.getTaskManager().createAccount("Shared_Account", 1000.0, 0.05);
            updateStatus("Initialized shared bank account with RMB 1000.00");
        } catch (Exception e) {
            updateStatus("Error initializing bank account: " + e.getMessage());
        }
    }

    private void setupMainUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 800);
        setLayout(new BorderLayout());

        add(createHeaderPanel(), BorderLayout.NORTH);
        add(createMainPanel(), BorderLayout.CENTER);
        add(createStatusPanel(), BorderLayout.SOUTH);

        setLocationRelativeTo(null);
    }

    private JPanel createHeaderPanel() {
        JPanel headerPanel = new JPanel();
        headerPanel.setBackground(new Color(51, 153, 255));
        headerPanel.setPreferredSize(new Dimension(1200, 80));
        headerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        JLabel titleLabel = new JLabel("Integrated Management System");
        titleLabel.setFont(new Font("Arial", Font.BOLD, 32));
        titleLabel.setForeground(Color.WHITE);
        headerPanel.add(titleLabel);

        return headerPanel;
    }

    private JPanel createMainPanel() {
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setFont(new Font("Arial", Font.PLAIN, 16));

        // Zoo Management Tab
        JPanel zooPanel = zooSystem.createMainPanel();
        tabbedPane.addTab("Zoo Management", zooPanel);

        // Bank Management Tab
        JPanel bankPanel = bankSystem.getMainPanel();
        tabbedPane.addTab("Bank Management", bankPanel);

        // Restaurant Management Tab
        JPanel restaurantPanel = restaurantSystem.createMainPanel();
        tabbedPane.addTab("Restaurant Management", restaurantPanel);

        // Shape Analysis Tab
        JPanel shapePanel = shapeSystem.createMainPanel();
        tabbedPane.addTab("Shape Analysis", shapePanel);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        mainPanel.add(tabbedPane, BorderLayout.CENTER);

        return mainPanel;
    }

    private JPanel createStatusPanel() {
        JPanel statusPanel = new JPanel(new BorderLayout());
        statusPanel.setPreferredSize(new Dimension(1200, 30));
        statusPanel.setBorder(BorderFactory.createEtchedBorder());

        statusLabel = new JLabel("System Status: Running normally");
        statusLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
        statusPanel.add(statusLabel, BorderLayout.WEST);

        return statusPanel;
    }

    private void updateStatus(String message) {
        SwingUtilities.invokeLater(() -> statusLabel.setText("System Status: " + message));
    }

    public void handleZooTransaction(double amount) {
        try {
            bankSystem.getTaskManager().deposit("Shared_Account", amount);
            updateStatus("Zoo transaction of RMB " + amount + " deposited to bank.");
        } catch (Exception e) {
            updateStatus("Error processing zoo transaction: " + e.getMessage());
        }
    }

    public void handleRestaurantTransaction(double amount) {
        try {
            bankSystem.getTaskManager().deposit("Shared_Account", amount);
            updateStatus("Restaurant transaction of RMB " + amount + " deposited to bank.");
        } catch (Exception e) {
            updateStatus("Error processing restaurant transaction: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            UIIntegration system = new UIIntegration();
            system.setVisible(true);
        });
    }
}